package com.qinker.oa.controller;

import com.github.pagehelper.PageInfo;
import com.qinker.oa.message.CodeMsg;
import com.qinker.oa.message.Result;
import com.qinker.oa.model.User;
import com.qinker.oa.model.WorkInfo;
import com.qinker.oa.params.UserResult;
import com.qinker.oa.service.WorkInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class WorkInfoController {

    @Autowired
    private WorkInfoService workInfoService;

    @RequestMapping("/user/group/{page}")
    @ResponseBody
    public Result<PageInfo<UserResult>> getUserListGroupByWork(@PathVariable(name = "page") int page){
        PageInfo<UserResult> info = workInfoService.getUserListGroupByWork(page, 10);
        return Result.success(info);
    }


    @RequestMapping("/workInfo/{id}")
    @ResponseBody
    public Result<List<WorkInfo>> getWorkInfoList(@PathVariable int id){
        return Result.success(workInfoService.getWorkInfoList(id));
    }

    @RequestMapping("/workInfo/add")
    @ResponseBody
    public Result<CodeMsg> addWorkInfo(@RequestParam String content, HttpSession session){
        User user = (User)session.getAttribute("user");
        WorkInfo workInfo = WorkInfo.builder().uId(user.getId()).content(content).build();
        workInfoService.addWorkInfo(workInfo);
        return Result.successNoData(CodeMsg.SAVE_SUCCESS);
    }
}
